<?php

namespace app\common\model\circle;

use think\Model;
use think\Db;

class User extends Model
{

    // 设置当前模型使用的完整数据库连接信息
    protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'circle_user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
	    'avatar',
	    'nickname',
	    'number',
        'identity_text',
        //'state_text',
        'is_auth_text',
        'is_ban_text',
        'sh_time_text'
    ];


	public function getNicknameAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('nickname');

		return $value ? $value : '';

	}


	public function getNumberAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('number');

		return $value ? $value : '';

	}


	public function getAvatarAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');

		return $value ? $value : '';

	}

    
    public function getIdentityList()
    {
        return ['1' => __('吧主'), '2' => __('管理员'), '3' => __('圈友')];
    }

//    public function getStateList()
//    {
//        return ['1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3')];
//    }

    public function getIsAuthList()
    {
        return ['1' => __('正常'), '2' => __('已移出黑名单')];
    }

    public function getIsBanList()
    {
        return ['1' => __('已封禁'), '2' => __('正常')];
    }


    public function getIdentityTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['identity']) ? $data['identity'] : '');
        $list = $this->getIdentityList();
        return isset($list[$value]) ? $list[$value] : '';
    }


//    public function getStateTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
//        $list = $this->getStateList();
//        return isset($list[$value]) ? $list[$value] : '';
//    }


    public function getIsAuthTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_auth']) ? $data['is_auth'] : '');
        $list = $this->getIsAuthList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsBanTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_ban']) ? $data['is_ban'] : '');
        $list = $this->getIsBanList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sh_time']) ? $data['sh_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setShTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function circle()
    {
        return $this->belongsTo('app\common\model\Circle', 'circle_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

	public function users()
	{
		return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}


	//获取用户信息
	public function getuser($circle,$user_id){

		$data = [];

		$data['level'] = $circle->level;
		$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle->level])->find();

		$nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$circle->circle_id])->value('nickname');
		$data['level_name'] = $nickname?$nickname:$level['name'];

		$data['icon'] = cdnurl($level['image'],true);
		$data['bg_color'] = $level['bg_color'];
		$data['font_color'] = $level['font_color'];
		$data['identity'] = $circle->identity;

		return $data;
	}



}
